﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

public class GestionUsuarios
{

    public bool GrabarUsuario(Usuario usr)
    {
        AccesoBD datos = new AccesoBD();
        SqlConnection cn = datos.ObtenerConexion();
        if (cn != null)
        {
            SqlCommand cmd;
            String sql = "Insert into Usuarios values(";
            sql += "'" + usr.CorreoElectronico + "',";
            sql += "'" + usr.Contrasenia + "',";
            sql += "'" + usr.Nombre + "',";
            sql += "'" + usr.Apellido + "',";
            sql += "'" + usr.Alias + "',";
            sql += "'" + usr.Nacionalidad + "',";
            sql += "'" + usr.Sexo + "',";
            sql += "'" + usr.FechaNac + "',";
            sql += "'" + usr.Telefono + "',";
            sql += "'" + usr.Domicilio + "',";
            sql += "'1')";
            cmd = new SqlCommand(sql, cn);
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (SqlException)
            {
                return false;
            }
            finally
            {
                cn.Close();
            }
        }
        else
            return false;
    }

    public bool EstaRegistrado(String correoElectronico)
    {
        AccesoBD datos = new AccesoBD();
        SqlConnection cn = datos.ObtenerConexion();
        SqlCommand cmd;
        SqlDataReader dr;
        String sql = "Select * From Usuarios WHERE correo_electronico = '" + correoElectronico + "'";
        if (cn != null)
        {
            cmd = new SqlCommand(sql, cn);
            try
            {
                dr = cmd.ExecuteReader();
                if (dr.Read())
                    return true;
                else
                    return false;
            }
            catch (SqlException)
            {
                return false;
            }
            finally
            {
                cn.Close();
            }
        }
        else
            return false;
    }

    public void BajaUsuario(String correoElectronico, String clave, ref bool Borrado)
    {
        AccesoBD datos = new AccesoBD();
        SqlConnection cn = datos.ObtenerConexion();
        SqlCommand cmd;
        SqlDataReader dr;
        String sql = "Select * From Usuarios WHERE correo_electronico = '" + correoElectronico + "' AND contrasenia='" + clave + "'";
        if (cn != null)
        {
            cmd = new SqlCommand(sql, cn);
            try
            {
                dr = cmd.ExecuteReader();
                if (dr.Read()){
                    dr.Close();
                    sql = "UPDATE Usuarios SET estado=0 WHERE correo_electronico='" + correoElectronico + "' AND contrasenia='" + clave + "'";
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                    Borrado = true;
          
                }
                else
                    return;
            }
            catch (SqlException)
            {
                return;
            }
            finally
            {
                cn.Close();
            }
        }
        else
            return;

    }

    public bool Loguearse(String correoElectronico,String clave)
    {
        AccesoBD datos = new AccesoBD();
        SqlConnection cn = datos.ObtenerConexion();
        SqlCommand cmd;
        SqlDataReader dr;
        String sql = "Select * From Usuarios WHERE correo_electronico = '" + correoElectronico + "' AND contrasenia='" + clave + "'";
        if (cn != null)
        {
            cmd = new SqlCommand(sql, cn);
            try
            {
                dr = cmd.ExecuteReader();
                if (dr.Read())
                    return true;
                else
                    return false;
            }
            catch (SqlException)
            {
                return false;
            }
            finally
            {
                cn.Close();
            }
        }
        else
            return false;
    }
}